{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tornado\n",
    "from tornado.web import RequestHandler\n",
    "import json\n",
    "\n",
    "\n",
    "class MainHandler(RequestHandler):\n",
    "\n",
    "    ## 入口\n",
    "    ## 用于初始化handler类的过程\n",
    "    ## 不能使用协程 async 关键字\n",
    "    def initialize(self, db):\n",
    "        self.db_name = db\n",
    "\n",
    "    ## prepare 方法用于真正调用请求处理之前的初始化方法\n",
    "    def prepare(self):\n",
    "        ## 打印日志。。。\n",
    "        ## 打开文件\n",
    "        pass\n",
    "\n",
    "    def on_finish(self):\n",
    "        ## 关闭句柄，清理内存\n",
    "        pass\n",
    "\n",
    "    ## http方法\n",
    "    def get(self, *args, **kwargs):\n",
    "        #####################################################\n",
    "        ## 获取查询字符串的值\n",
    "        data1 = self.get_query_argument('name')\n",
    "        ## 传参：http://127.0.0.1:8000/?name=bobby&name=bobby2\n",
    "        ## 取值：<class 'list'>:['bobby', 'bobby2]\n",
    "        data2 = self.get_query_arguments('name')\n",
    "\n",
    "        pass\n",
    "\n",
    "    def post(self, *args, **kwargs):\n",
    "        #####################################################\n",
    "        ## 接收post的参数\n",
    "        data1 = self.get_argument(\"name\")\n",
    "        data2 = self.get_arguments(\"name\")\n",
    "        #####################################################\n",
    "        ## 获取所有的参数（字典）\n",
    "        data1 = self.request.arguments(\"name\")\n",
    "        #####################################################\n",
    "        ##\n",
    "        # param = self.request.body.decode(\"utf-8\")\n",
    "        # data1 = json.loads(param)\n",
    "\n",
    "        #####################################################\n",
    "        ## 请求方式如下：\n",
    "        ## headers = {\"Content-Type\": \"application/x-www-form-urlencoded;\"}\n",
    "        ## requests.post(\"http://127.0.0.1:8000/?name=bobby\", headers=headers, data = {\n",
    "        ## \"name\": \"bobby2\", \"age\": 28,\n",
    "        ## })\n",
    "        try:\n",
    "            data1 = self.get_body_argument(\"name\")\n",
    "            data2 = self.get_body_arguments(\"name\")\n",
    "        except Exception as e:\n",
    "            ## 输出状态码\n",
    "            self.set_status(500)\n",
    "\n",
    "        self.write(\"hello\")\n",
    "        ## 终止向客户端发送数据\n",
    "        self.finish()\n",
    "        ## finish()还可以返回json数据\n",
    "        self.finish({\n",
    "            \"name\": \"bobby\"\n",
    "        })\n",
    "        self.redirect()\n",
    "\n",
    "    def delete(self, *args, **kwargs):\n",
    "        pass\n",
    "\n",
    "    def patch(self, *args, **kwargs):\n",
    "        pass\n",
    "\n",
    "    ## 自定义错误页面\n",
    "    def write_error(self, status_code, **kwargs):\n",
    "        pass\n",
    "\n",
    "    ###############################################\n",
    "    ## 常用输出\n",
    "    ## set_status, write, finish, redirect, write_error\n",
    "\n",
    "urls = [\n",
    "    tornado.web.URLSpec(\"/\", MainHandler, name=\"index\"),\n",
    "]\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app = tornado.web.Application(urls, debug=True, autoreload=True)\n",
    "    # options 变量使用\n",
    "    app.listen(8000)\n",
    "    tornado.ioloop.IOLoop.current().start()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
